//
//  DDMyQuestionCollectionViewCell.m
//  dingdang
//
//  Created by Chen Jing on 15-7-23.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDMyQuestionCollectionViewCell.h"

@interface DDMyQuestionCollectionViewCell (){
    NSMutableParagraphStyle * paragraphStyle;
    CGRect holdRect;
    CGRect holdCourseLabelFrame;
    CGRect holdContentOfRewardFrame;
}

@end

@interface DDMyQuestionCollectionViewCell ()



@property (weak, nonatomic) IBOutlet UIImageView *imageView_1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_3;
@end

@implementation DDMyQuestionCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    self.label_passward.layer.cornerRadius = 5;
    self.label_passward.layer.borderColor = [UIColor colorWithRed:33/255.0 green:172/255.0 blue:229/255.0 alpha:1].CGColor;
    self.label_passward.layer.borderWidth = 1;


    float btnWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]) / 4.0;
    float btnHeight = CGRectGetHeight(self.contentOfRepublishAndOtherView.frame);
    self.btn_answerCount.frame = CGRectMake(0, 0, btnWidth, btnHeight);
    self.btn_updatePassword.frame = CGRectMake(btnWidth, 0, btnWidth, btnHeight);
    self.btn_republish.frame = CGRectMake(2 * btnWidth, 0, btnWidth, btnHeight);
    self.btn_releaseAnswer.frame = CGRectMake(3 * btnWidth, 0, btnWidth, btnHeight);

    self.imageView_1.center = CGPointMake(btnWidth, btnHeight / 2.0);
    self.imageView_2.center = CGPointMake(2 * btnWidth, btnHeight / 2.0);
    self.imageView_3.center = CGPointMake(3 * btnWidth, btnHeight / 2.0);

    paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 1;
    self.imageView_body.frame = CGRectMake(8, 79, CGRectGetWidth([[UIScreen mainScreen] bounds]) - 16, 0);
}



- (IBAction)btnClick_answerCount:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(pressBtn_answerCount:)]) {
        [self.delegate performSelector:@selector(pressBtn_answerCount:) withObject:self];
    }
}
- (IBAction)btnClick_updatePwd:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(pressBtn_updatePwd:)]) {
        [self.delegate performSelector:@selector(pressBtn_updatePwd:) withObject:self];
    }
}
- (IBAction)btnClick_republish:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(pressBtn_republish:)]) {
        [self.delegate performSelector:@selector(pressBtn_republish:) withObject:self];
    }
}
- (IBAction)btnClick_releaseAnswer:(id)sender {
    
    //ruanch bug:3.	我的发问，点击“公开答案”反应时间太久，用户会以为没有点到，反复点；
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    if ([button.titleLabel.text isEqualToString:@"公开答案"]) {
        [button setTitle:@"隐藏答案" forState:UIControlStateNormal];
    }else{
        [button setTitle:@"公开答案" forState:UIControlStateNormal];
    }
    //ruanch bug end
    
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(pressBtn_publishAnswer:)]) {
        [self.delegate performSelector:@selector(pressBtn_publishAnswer:) withObject:self];
    }
}
- (IBAction)btnClick_changeCourse:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(pressBtn_changeCourse:)]) {
        [self.delegate performSelector:@selector(pressBtn_changeCourse:) withObject:self];
    }
}


-(void)fixCell{
    if (self.info_publishType == DDType_question_questionPublishType_Encryption) {
        self.label_passward.hidden = NO;
        self.contentOfRepublishAndOtherView.hidden = NO;
        self.contentOfRepublishView.hidden = YES;

        self.contentOfReward.hidden = YES;

    }else if(self.info_publishType == DDType_question_questionPublishType_Reward){
        self.label_passward.hidden = YES;
        self.contentOfRepublishAndOtherView.hidden = YES;
        self.contentOfRepublishView.hidden = NO;

        self.contentOfReward.hidden = NO;

    }else{
        self.label_passward.hidden = YES;
        self.contentOfRepublishAndOtherView.hidden = YES;
        self.contentOfRepublishView.hidden = NO;

        self.contentOfReward.hidden = YES;
    }

    if (holdRect.origin.y == 0) {
        holdRect = self.label_body.frame;
        holdCourseLabelFrame = self.label_course.frame;
        holdContentOfRewardFrame = self.contentOfReward.frame;
        
    }
    
    
    
    
    self.label_body.frame = holdRect;
    
    self.label_course.frame = holdCourseLabelFrame;
    self.imageView_course_bg.frame = holdCourseLabelFrame;
    
    self.contentOfReward.frame = holdContentOfRewardFrame;

    
    CGRect tempCourse = [self.label_course.text boundingRectWithSize:CGSizeMake(MAXFLOAT, holdCourseLabelFrame.size.height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.label_course.font,NSParagraphStyleAttributeName : paragraphStyle} context:nil];
    float tempAddWith = tempCourse.size.width;
    if (tempAddWith > CGRectGetWidth([[UIScreen mainScreen] bounds]) - 220) {
        tempAddWith = CGRectGetWidth([[UIScreen mainScreen] bounds]) - 220;
    }else if(tempAddWith < holdCourseLabelFrame.size.width) {
        tempAddWith = holdCourseLabelFrame.size.width;
    }
    
    self.label_course.frame = CGRectMake(holdCourseLabelFrame.origin.x + holdCourseLabelFrame.size.width - tempAddWith, holdCourseLabelFrame.origin.y, tempAddWith, holdCourseLabelFrame.size.height);
    self.imageView_course_bg.frame = CGRectMake(self.label_course.frame.origin.x - 3, self.label_course.frame.origin.y, self.label_course.frame.size.width + 3, self.label_course.frame.size.height);
    
    self.contentOfReward.frame = CGRectMake(holdContentOfRewardFrame.origin.x - tempAddWith - 3 + holdCourseLabelFrame.size.width, holdContentOfRewardFrame.origin.y, holdContentOfRewardFrame.size.width, holdContentOfRewardFrame.size.height);
    
    UIImage * tempImage =  [self.imageView_course_bg.image stretchableImageWithLeftCapWidth:floorf(self.imageView_course_bg.image.size.width/2) topCapHeight:floorf(self.imageView_course_bg.image.size.height/2)];
    self.imageView_course_bg.image = tempImage;
    
    
    CGRect tempRect = [self.label_body.text boundingRectWithSize:CGSizeMake(CGRectGetWidth([[UIScreen mainScreen] bounds]) - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15],NSParagraphStyleAttributeName : paragraphStyle} context:nil];

    self.label_body.frame = CGRectMake(CGRectGetMinX(self.label_body.frame),
                                       CGRectGetMinY(self.label_body.frame),
                                       CGRectGetWidth([[UIScreen mainScreen] bounds]) - 20,
                                       CGRectGetHeight(self.label_body.frame) + (tempRect.size.height - 21));

    self.imageView_body.frame = CGRectMake(CGRectGetMinX(self.imageView_body.frame),
                                           CGRectGetMinY(self.label_body.frame) + CGRectGetHeight(self.label_body.frame) + 10,
                                           CGRectGetWidth(self.imageView_body.frame),
                                           CGRectGetHeight(self.imageView_body.frame));


}

@end
